package com.definity.toolkit.ui.widget;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseMotionListener;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.border.Border;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.plaf.basic.BasicInternalFrameUI;

import com.definity.toolkit.ui.Body;
import com.definity.toolkit.ui.widget.paint.Paint;

public class DesktopPane extends JDesktopPane implements Widget<DesktopPane>, WidgetPaint<DesktopPane> {

	private static final long serialVersionUID = 4482404492999178125L;

	private Paint paint;

	public DesktopPane() {

	}
	
	@Override
	public String id() {
		return getName();
	}

	@Override
	public DesktopPane id(String id) {
		setName(id);
		return this;
	}

	@Override
	public DesktopPane properties(Body<DesktopPane> body) {
		body.content(this);
		return this;
	}

	@Override
	public Color background() {
		return getBackground();
	}

	@Override
	public DesktopPane background(Color background) {
		setBackground(background);
		return this;
	}

	@Override
	public Color foreground() {
		return getForeground();
	}

	@Override
	public DesktopPane foreground(Color foreground) {
		setForeground(foreground);
		return this;
	}

	@Override
	public DesktopPane size(int width, int height) {
		setPreferredSize(new Dimension(width, height));
		return this;
	}

	@Override
	public Font font() {
		return getFont();
	}

	@Override
	public DesktopPane font(Font font) {
		setFont(font);
		return this;
	}
	
	public DesktopPane add(Component c) {
		if (c instanceof JInternalFrame) {
			add((JInternalFrame) c);
		} else {
			super.add(c);
		}
		return this;
	}
	
	public DesktopPane add(JInternalFrame f) {
		f.setMaximizable(false);
		f.setIconifiable(false);
		f.setResizable(false);
		f.setClosable(false);
		f.addInternalFrameListener(new ToolkitInternalFrameListener());
		
		BasicInternalFrameUI ui = (BasicInternalFrameUI) f.getUI();

		Component north = ui.getNorthPane();
		if (north != null) {
			MouseMotionListener[] actions =
			(MouseMotionListener[]) north.getListeners(MouseMotionListener.class);
	
			for (int i = 0; i < actions.length; i++) {
				north.removeMouseMotionListener(actions[i]);
			}
		}
		
		super.add(f);
		
		return this;
	}

	public Border border() {
		return getBorder();
	}
	
	public DesktopPane border(Border border) {
		setBorder(border);
		return this;
	}

	public DesktopPane paint(Paint paint) {
		this.paint = paint;
		return this;
	}

	@Override
	protected void paintComponent(Graphics g) {
		if (paint != null)
			paint.paint((Graphics2D) g, this);
		super.paintComponent(g);
	}

	public DesktopPane openInCenter(Component comp) {
		centralizeComponent(comp);
		add(comp);
		return this;
	}
	
	public void centralizeComponent(Component component) {
		Dimension size = getSize();

		int x = (size.width / 2) - (component.getWidth() / 2);
		int y = (size.height / 2) - (component.getHeight() / 2);

		component.setLocation(x, y);
	}
	
	public DesktopPane close(InternalFrame frame) {
		frame.close();
		
		super.remove(frame);
		
		return this;
	}

	private class ToolkitInternalFrameListener implements InternalFrameListener {
	
		@Override
		public void internalFrameActivated(InternalFrameEvent event) {
			
		}
	
		@Override
		public void internalFrameClosed(InternalFrameEvent event) {
			
		}
	
		@Override
		public void internalFrameClosing(InternalFrameEvent event) {
			
		}
	
		@Override
		public void internalFrameDeactivated(InternalFrameEvent event) {
			
		}
	
		@Override
		public void internalFrameDeiconified(InternalFrameEvent event) {
			
		}
	
		@Override
		public void internalFrameIconified(InternalFrameEvent event) {
			
		}
	
		@Override
		public void internalFrameOpened(InternalFrameEvent event) {
			
		}
		
	}
	
}
